/*
' Copyright (c) 2011  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using System.Web.UI.WebControls;


namespace DotNetNuke.Modules.SocialHelpers {

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : SocialHelpersSettingsBase {

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings() {
            try {
                string fbScript = System.IO.File.ReadAllText(Server.MapPath("~/desktopmodules/socialhelpers/facebook.txt"));
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "fbsdk", fbScript, false);
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "linkedinsdk", "<script src=\"http://platform.linkedin.com/in.js\" type=\"text/javascript\"></script>", false);
                Page.ClientScript.RegisterClientScriptInclude("jquery.tweet", Page.ResolveUrl("~/desktopmodules/socialhelpers/scripts/jquery.tweet.js"));
                if (Page.IsPostBack == false) {
                    //Check for existing settings and use those on this page
                    
                    txtFBAppId.Text = GetString("FBAppId", String.Empty);
                    txtFBPage.Text = GetString("FBPage", String.Empty);
                    
                    string PluginName = GetString("PluginName", String.Empty);
                    LoadPlugins(PluginName);
                    PluginHTML.Text = GetString("PluginHTML", String.Empty);
                 
                    
                }
            } catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings() {
            try {
                ModuleController modules = new ModuleController();
                modules.UpdateTabModuleSetting(this.TabModuleId, "FBAppId", txtFBAppId.Text);
                modules.UpdateTabModuleSetting(this.TabModuleId, "FBPage", txtFBPage.Text);
                modules.UpdateTabModuleSetting(this.TabModuleId, "PluginName", drpPlugins.SelectedItem.Value);
                modules.UpdateTabModuleSetting(this.TabModuleId, "PluginHTML", PluginHTML.Text);
                
                //modules.UpdateModuleSetting(this.TabModuleId, "LogBreadCrumb", (control.value ? "true" : "false"));
            } catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion
        public void LoadPlugins(string PluginName) {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("~/DesktopModules/SocialHelpers/plugins/"));
            ListItem li = new ListItem("", String.Empty);
            try {
                foreach (System.IO.FileInfo d in di.GetFiles("*.txt")) {
                    li = new ListItem(d.Name.Replace("-"," ").Replace(".txt",""), d.Name);
                    if (d.Name == PluginName) {
                        li.Selected = true;
                    }
                    drpPlugins.Items.Add(li);
                }
            } catch {

            }
        }
        private string GetString(string key, string defaultValue) {
            if (Settings.ContainsKey(key)) {
                if (!String.IsNullOrEmpty(Settings[key].ToString())) {
                    return Settings[key].ToString();
                }
            }
            return defaultValue;
        }
    }

}

